package com.hosp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hosp.bean.Role;
import com.hosp.bean.RoleExample;
import com.hosp.bean.Sysuser;
import com.hosp.bean.SysuserExample;
import com.hosp.controller.dto.UserDTO;
import com.hosp.mapper.RoleMapper;
import com.hosp.mapper.SysuserMapper;
import com.hosp.vo.PageRequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author ztranscript 2021/9/27 21:18
 **/
@Slf4j
@Service
public class UserService {
    @Autowired
    private SysuserMapper sysuserMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询列表
     *
     * @param sysuser     查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    public PageInfo<UserDTO> list(Sysuser sysuser, PageRequestVO pageRequest) {
        SysuserExample example = new SysuserExample();
        SysuserExample.Criteria criteria = example.createCriteria();
        String name = sysuser.getuTrueName();
        if (!StringUtils.isEmpty(name)) {
            criteria.andUTrueNameLike("%" + name + "%");
        }
        // 分页查询
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<Sysuser> sysusers = sysuserMapper.selectByExample(example);
        PageInfo<Sysuser> info = new PageInfo<>(sysusers);

        List<UserDTO> userDTOList = new ArrayList<>();
        sysusers.forEach(user -> {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            Integer rid = user.getrId();
            if (rid != null) {
                // 查询对应的角色信息
                RoleExample roleExample = new RoleExample();
                RoleExample.Criteria roleExampleCriteria = roleExample.createCriteria();
                roleExampleCriteria.andRIdEqualTo(rid);
                List<Role> roles = roleMapper.selectByExample(roleExample);
                if (!CollectionUtils.isEmpty(roles)) {
                    userDTO.setRole(roles.get(0));
                }
            }
            userDTOList.add(userDTO);
        });

        PageInfo<UserDTO> dtoPageInfo = new PageInfo<>(userDTOList);
        BeanUtils.copyProperties(info, dtoPageInfo, "list");
        return dtoPageInfo;
    }


    /**
     * 保存
     *
     * @param sysuser 保存对象
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Sysuser sysuser) {
        Integer uid = sysuser.getuId();
        if (Objects.isNull(uid)) {
            // add
            sysuserMapper.insertSelective(sysuser);
        } else {
            // update
            sysuserMapper.updateByPrimaryKeySelective(sysuser);
        }

        return true;
    }

    /**
     * 主键查询
     *
     * @param uId 查询条件
     * @return 响应
     */
    public UserDTO selectById(Integer uId) {
        Sysuser user = sysuserMapper.selectByPrimaryKey(uId);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        Integer rid = user.getrId();
        if (rid != null) {
            // 查询对应的角色信息
            RoleExample roleExample = new RoleExample();
            RoleExample.Criteria roleExampleCriteria = roleExample.createCriteria();
            roleExampleCriteria.andRIdEqualTo(rid);
            List<Role> roles = roleMapper.selectByExample(roleExample);
            if (!CollectionUtils.isEmpty(roles)) {
                userDTO.setRole(roles.get(0));
            }
        }
        return userDTO;
    }

    /**
     * 删除
     *
     * @param uIds 待删除 IDs
     * @return 响应
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Integer> uIds) {
        int sum = 0;
        for (Integer uId : uIds) {
            // user
            int row = sysuserMapper.deleteByPrimaryKey(uId);
            sum += row;
        }
        return sum == uIds.size();
    }
}
